//
//  GPSearchViewController.m
//  gupiao
//
//  Created by mac on 2019/8/14.
//  Copyright © 2019 mac. All rights reserved.
//

#import "GPSearchViewController.h"
#import "GPSearchListCell.h"
#import "GPSearchNetHelper_NN.h"
#import "GPSearchModel.h"


@interface GPSearchViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (nonatomic,strong) NSArray *sourceArr;
@end

@implementation GPSearchViewController

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.searchTextField becomeFirstResponder];
}

-(void)searchAction:(NSString*)text{
    [self showLoadingAnimatedToView:self.view];
    [GPSearchNetHelper_NN getSearchResult:text success:^(id response, BOOL isNetData) {
        NSLog(@"%@",response);
        [self dismissLoadingAnimatedToView];
        if ([response[@"error_code"] integerValue]==0) {
            self.sourceArr = response[@"result"];
            [self.tableView reloadData];
        }
    } fail:^(NSError *error, NSURLSessionDataTask *task) {
        [self dismissLoadingAnimatedToView];
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self searchAction:textField.text];
    return YES;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GPSearchListCell *cell = [GPSearchListCell cellWithTableView:tableView :indexPath];
    GPSearchModel *model = [GPSearchModel mj_objectWithKeyValues:self.sourceArr[indexPath.row][@"data"]];
    cell.model = model;
    cell.gid.text = model.gid;
    cell.name.text = model.name;
    cell.competitivePri.text = model.competitivePri;
    cell.reservePri.text = model.reservePri;
    float increPer = [model.increPer floatValue];
    cell.increPer.text = [NSString stringWithFormat:@"%@%@%@",increPer<0?@"":@"+",model.increPer,@"%"];
    cell.increPer.textColor = increPer<0?UIColorFromRGB(0x00CB3B):UIColorFromRGB(0xFF0000);
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63;
}

-(void)setUp{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"GPSearchListCell" bundle:nil] forCellReuseIdentifier:@"GPSearchListCell"];
    self.searchTextField.delegate = self;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
